package com.ymatou.shoppingcart.api.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.shoppingcart.api.parameter.GetShoppingCartPriceBean;
import com.ymatou.shoppingcart.api.service.GetShoppingCartPriceCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.data.Price;
import com.ymttest.business.database.ActivityInfo;
import com.ymttest.business.database.ProductInfo;
import com.ymttest.business.service.ShoppingCartCallServices;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 购物车中当前总金额和运费 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetShoppingCartPrice {
	private static GetShoppingCartPriceBean pricebean;
	private static GetShoppingCartPriceCall pricecall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("购物车中当前总金额和运费");
	}

	@Before
	public void caseUp() {
		pricebean = new GetShoppingCartPriceBean();
		pricecall = new GetShoppingCartPriceCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(pricecall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_001() {
		Logger.start(true,"获取购物车中当前总金额和运费成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID:" + uid);
			pricebean.setUserID(uid);
			Logger.comment("setAreaCode:" + "568");
			pricebean.setAreaCode("568");
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			Logger.comment(activityInfo.toString());
			// 不输入 userLevel fPromotion1
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, 0, false);
			Logger.verifyEquals(Float.valueOf(actprice.toString()), Float.valueOf(pricecall.getPrice()), "验证返回Price");
			// 运费,可能会出现精度不一致情况，转换成double比较
			if (activityInfo.get("fFlight") != null) {
				Logger.verifyEquals(Double.valueOf(activityInfo.get("fFlight").toString()),
						Double.valueOf(pricecall.getFreight()), "验证返回Freight");
			} else {
				Logger.debug(activityInfo.toString());
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_002() {
		Logger.start(true,"不输入userid获取购物车中当前总金额和运费失败");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			Map m = ShoppingCartCallServices.addCatalogToCart(uid);
			// Logger.comment("setUserID:"+uid);
			// pricebean.setUserID(uid);
			Logger.comment("setAreaCode:" + "568");
			pricebean.setAreaCode("568");
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, pricecall.getMsg().contains(ECode.ERROR400_MSG_1), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_003() {
		Logger.start(true,"不输入areacode获取购物车中当前总金额和运费失败");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			Map m = ShoppingCartCallServices.addCatalogToCart(uid);
			Logger.comment("setUserID:" + uid);
			pricebean.setUserID(uid);
			// Logger.comment("setAreaCode:"+"568");
			// pricebean.setAreaCode("568");
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, pricecall.getMsg().contains(ECode.ERROR400_MSG_2), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_004() {
		Logger.start(true,"输入userid和GuestUserID查询合并后购物车中当前总金额和运费成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			Map m = ShoppingCartCallServices.addCatalogToCart(uid);
			String uid2 = TestDataManager.getRandomUUID().toString();
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid2);
			Map m2 = ShoppingCartCallServices.addCatalogToCart(uid2);
			Logger.comment("setUserID:" + uid);
			pricebean.setUserID(uid);
			Logger.comment("setAreaCode:" + "568");
			pricebean.setAreaCode("568");
			Logger.comment("setGuestUserID:" + uid2);
			pricebean.setGuestUserID(uid2);
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			if (m.get("fFlight") != null && m2.get("fFlight") != null) {
				Double freight = Double.valueOf((new BigDecimal(m.get("fFlight").toString()).add(new BigDecimal(m2.get(
						"fFlight").toString()))).toString());
				Logger.verifyEquals(freight, Double.valueOf(pricecall.getFreight()), "验证返回Freight");
			}
			Logger.comment("计算实际金额");
			Logger.comment(m.toString());
			Logger.comment(m2.toString());
			BigDecimal actprice = Price.getOneActprice(m, 0, false);
			BigDecimal actprice2 = Price.getOneActprice(m2, 0, false);
			Logger.comment("商品1价格" + actprice.toString());
			Logger.comment("商品2价格" + actprice2.toString());
			Logger.verifyEquals(Float.valueOf(actprice.add(actprice2).toString()), Float.valueOf(pricecall.getPrice()),
					"验证返回Price");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_005() {
		Logger.start(true,"输入IsUserNewbie为true获取购物车中当前总金额和运费为新人价格成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID:" + uid);
			pricebean.setUserID(uid);
			Logger.comment("setAreaCode:" + "568");
			pricebean.setAreaCode("568");
			Logger.comment("setIsUserNewbie:" + "true");
			pricebean.setIsUserNewbie("true");
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.comment(activityInfo.toString());
			Logger.verifyEquals(ECode.SUCCESS, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			JSONObject jo = new JSONObject(pricecall.getReturnData());
			JSONObject data = jo.getJSONObject("Data");
			data.get("Price");
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, 0, true);
			Logger.verifyEquals(Float.valueOf(actprice.toString()), Float.valueOf(pricecall.getPrice()), "验证返回Price");
			// 运费,可能会出现精度不一致情况，转换成double比较
			if (activityInfo.get("fFlight") != null) {
				Logger.verifyEquals(Double.valueOf(activityInfo.get("fFlight").toString()),
						Double.valueOf(pricecall.getFreight()), "验证返回Freight");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_006() {
		Logger.start(true,"输入UserLevel为3查询用户等级为3的优惠信息成功");
		try {
			String lv = "3";
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID:" + uid);
			pricebean.setUserID(uid);
			Logger.comment("setAreaCode:" + "568");
			pricebean.setAreaCode("568");
			Logger.comment("setUserLevel:" + lv);
			pricebean.setUserLevel(lv);
			;
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.comment(activityInfo.toString());
			Logger.verifyEquals(ECode.SUCCESS, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, Integer.valueOf(lv), false);
			Logger.verifyEquals(Float.valueOf(actprice.toString()), Float.valueOf(pricecall.getPrice()), "验证返回Price");
			// 运费,可能会出现精度不一致情况，转换成double比较
			if (activityInfo.get("fFlight") != null) {
				Logger.verifyEquals(Double.valueOf(activityInfo.get("fFlight").toString()),
						Double.valueOf(pricecall.getFreight()), "验证返回Freight");
			} else {
				Logger.debug(activityInfo.toString());
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetShoppingCartPrice_007() {
		Logger.start(true,"输入UserLevel为6查询用户等级为6的优惠信息成功");
		try {
			String lv = "6";
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			HashMap activityInfo = (HashMap) ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("setUserID:" + uid);
			pricebean.setUserID(uid);
			Logger.comment("setAreaCode:" + "568");
			pricebean.setAreaCode("568");
			Logger.comment("setUserLevel:" + lv);
			pricebean.setUserLevel(lv);
			;
			pricecall.setData(pricebean);
			pricecall.callService();
			Logger.comment("pricecall send:" + pricecall.getEntityString());
			Logger.comment("pricecall return:" + pricecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, pricecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, pricecall.getBCode(), "验证返回bcode");
			Logger.comment(activityInfo.toString());
			Logger.comment("计算实际金额");
			BigDecimal actprice = Price.getOneActprice(activityInfo, Integer.valueOf(lv), false);
			Logger.verifyEquals(Float.valueOf(actprice.toString()), Float.valueOf(pricecall.getPrice()), "验证返回Price");
			// 运费,可能会出现精度不一致情况，转换成double比较
			if (activityInfo.get("fFlight") != null) {
				Logger.verifyEquals(Double.valueOf(activityInfo.get("fFlight").toString()),
						Double.valueOf(pricecall.getFreight()), "验证返回Freight");
			} else {
				Logger.debug(activityInfo.toString());
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}